package org.vector.daoImpl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.vector.dao.BankDao;
import org.vector.utils.BaseDao;

public class BankDaoImpl implements BankDao {
	BaseDao bd = new BaseDao();
	/**
	 * 登录
	 */
	@Override
	public int login(int bankid,int pass) {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/bank?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
			String user = "root";
			String password = "root";
			Connection connection = DriverManager.getConnection(url,user,password);
			String sql = "select * from bank where bankid=? and password = ?";
			PreparedStatement ps = connection.prepareStatement(sql);
			//给sql中的？赋值
			ps.setInt(1, bankid);
			ps.setInt(2, pass);
			ResultSet resultset = ps.executeQuery();
			if(resultset.next()) {
				return 1;
			}else {
				return 0;
			}
		} catch ( Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	/**
	 * 注册
	 */
	@Override
	public int register(int bankid, String name, int password, int idCard, int ccd, int ccdpassword, int phone,int money) {
		String sql = "insert into bank values(?,?,?,?,?,?,?,?)";
		int rows = bd.executeUpdate(sql, bankid,name,password,idCard,ccd,ccdpassword,phone,money);
		return rows;
	}
	@Override
	public int yanzheng(int bankid, int ccd, int ccdpassword) {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/bank?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
			String user = "root";
			String password = "root";
			Connection connection = DriverManager.getConnection(url,user,password);
			String sql = "select * from bank where bankid=? and ccd = ? and ccdpassword = ?";
			PreparedStatement ps = connection.prepareStatement(sql);
			//给sql中的？赋值
			ps.setInt(1, bankid);
			ps.setInt(2, ccd);
			ps.setInt(3, ccdpassword);
			ResultSet resultset = ps.executeQuery();
			if(resultset.next()) {
				//System.out.println("登陆成功");
				return 1;
			}else {
				//System.out.println("用户名或密码错误");
				return 0;
			}
		} catch ( Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	

}
